home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 170 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.9 KB  |  351 lines

  1. Path: news.dx.net!NewsWatcher!user
  2. From: mikedorman@cedarnet.com (Mike Dorman)
  3. Newsgroups: comp.lang.c
  4. Subject: help debugging ISR function
  5. Date: Tue, 02 Jan 1996 16:48:00 -0500
  6. Organization: CedarNet Online
  7. Message-ID: <mikedorman-0201961648020001@205.148.200.151>
  8. NNTP-Posting-Host: 205.148.200.151
  9.  
  10. This is an ISR function for interrupts 0xB and 0xC (the serial port
  11. interrupts).  And for some reason, whenever it is called, it locks the
  12. computer.  I have no idea why.  Please take a look at my code and see if
  13. you get any ideas.  Thanks!
  14.  
  15. (BTW, this code *doesn't* use UART FIFOs!)
  16. ========================== begin ISR.INC ======================
  17. ISR_PROLOG      macro
  18.         push    ax                      ; save all registers
  19.         push    bx
  20.         push    cx
  21.         push    dx
  22.         push    es
  23.         push    ds
  24.         push    si
  25.         push    di
  26.         push    bp
  27.         mov     bp,DGRP                 ; DS data segment
  28.         mov     ds,bp
  29.         mov     bp,sp                   ; set up stack frame for locals
  30. endm
  31. ISR_EPILOG      macro
  32. ; restore all registers
  33.         pop     bp
  34.         pop     di
  35.         pop     si
  36.         pop     ds
  37.         pop     es
  38.         pop     dx
  39.         pop     cx
  40.         pop     bx
  41.         pop     ax
  42.         iret                           ; return =
  43. endm
  44. ============================ end ISR.INC ==========================
  45. ============================= begin COMISR.ASM =======================
  46. DGRP GROUP DSEG,BSEG
  47. DSEG SEGMENT BYTE PUBLIC 'IDATA'
  48. DSEG ENDS
  49. BSEG SEGMENT BYTE PUBLIC 'UDATA'
  50. BSEG ENDS
  51. CSEG SEGMENT BYTE PUBLIC 'CODE'
  52.  ASSUME CS:CSEG, DS:DGRP, SS:DGRP
  53.  EXTRN ?eq:NEAR,?ne:NEAR,?lt:NEAR,?le:NEAR,?gt:NEAR
  54.  EXTRN ?ge:NEAR,?ult:NEAR,?ule:NEAR,?ugt:NEAR,?uge:NEAR
  55.  EXTRN ?not:NEAR,?switch:NEAR,?temp:WORD
  56.  INCLUDE ISR.INC
  57.  PUBLIC _isr_off
  58. _isr_off: PUSH BP
  59.  MOV BP,SP
  60.  mov AX, OFFSET _isr
  61.  POP BP
  62.  RET
  63.  PUBLIC _isr_seg
  64. _isr_seg: PUSH BP
  65.  MOV BP,SP
  66.  mov AX, SEG _isr
  67.  POP BP
  68.  RET
  69. _isr: ISR_PROLOG
  70.  SUB SP,1
  71.  MOV AX,1
  72.  MOV DGRP:_tempflag,AX
  73.  MOV AX,33
  74.  PUSH AX
  75.  CALL _in
  76.  ADD SP,2
  77.  MOV -1[BP],AL
  78.  MOV AX,33
  79.  PUSH AX
  80.  MOV AL,DGRP:_pic_mask
  81.  NOT AL
  82.  OR AL,-1[BP]
  83.  XOR AH,AH
  84.  PUSH AX
  85.  CALL _out
  86.  ADD SP,4
  87.  MOV AX,32
  88.  PUSH AX
  89.  MOV AX,32
  90.  PUSH AX
  91.  CALL _out
  92.  ADD SP,4
  93.  MOV AX,DGRP:_g_com
  94.  ADD AX,2
  95.  PUSH AX
  96.  CALL _in
  97.  ADD SP,2
  98.  MOV -1[BP],AL
  99. ?1:
  100.  MOV AL,-1[BP]
  101.  AND AL,1
  102.  JZ ??1
  103.  JMP ?2
  104. ??1:
  105.  MOV AL,-1[BP]
  106.  AND AL,14
  107.  XOR AH,AH
  108.  MOV CL,1
  109.  SHR AX,CL
  110.  MOV BX,OFFSET ?4
  111.  JMP ?switch
  112. ?5:
  113.  MOV AX,DGRP:_g_com
  114.  ADD AX,5
  115.  PUSH AX
  116.  CALL _in
  117.  ADD SP,2
  118.  JMP ?3
  119. ?6:
  120.  MOV AX,DGRP:_g_in_end
  121.  INC AX
  122.  MOV DGRP:_g_in_end,AX
  123.  MOV CX,AX
  124.  MOV AX,4095
  125.  CALL ?gt
  126.  JNZ ??2
  127.  JMP ?7
  128. ??2:
  129.  MOV AX,-1
  130.  MOV DGRP:_g_in_end,AX
  131. ?7:
  132.  MOV AX,DGRP:_g_in_end
  133.  INC AX
  134.  MOV DGRP:_g_in_end,AX
  135.  DEC AX
  136.  MOV AX,DGRP:_g_in_end
  137.  MOV SI,OFFSET DGRP:_g_serial_in
  138.  ADD SI,AX
  139.  PUSH SI
  140.  MOV AX,DGRP:_g_com
  141.  PUSH AX
  142.  CALL _in
  143.  ADD SP,2
  144.  POP BX
  145.  MOV [BX],AL
  146.  JMP ?3
  147. ?8:
  148.  MOV AX,DGRP:_g_com
  149.  PUSH AX
  150.  CALL _in
  151.  ADD SP,2
  152.  JMP ?3
  153. ?9:
  154.  MOV AX,DGRP:_g_out_beg
  155.  MOV CX,DGRP:_g_out_end
  156.  CALL ?ne
  157.  JNZ ??3
  158.  JMP ?10
  159. ??3:
  160.  MOV AX,DGRP:_g_out_beg
  161.  INC AX
  162.  MOV DGRP:_g_out_beg,AX
  163.  MOV CX,AX
  164.  MOV AX,4095
  165.  CALL ?gt
  166.  JNZ ??4
  167.  JMP ?11
  168. ??4:
  169.  MOV AX,-1
  170.  MOV DGRP:_g_out_beg,AX
  171. ?11:
  172.  MOV AX,DGRP:_g_out_beg
  173.  INC AX
  174.  MOV DGRP:_g_out_beg,AX
  175.  DEC AX
  176.  MOV AX,DGRP:_g_com
  177.  PUSH AX
  178.  MOV AX,DGRP:_g_out_beg
  179.  MOV SI,OFFSET DGRP:_g_serial_out
  180.  ADD SI,AX
  181.  MOV AL,[SI]
  182.  CBW
  183.  PUSH AX
  184.  CALL _out
  185.  ADD SP,4
  186.  MOV AX,DGRP:_g_out_beg
  187.  MOV CX,DGRP:_g_out_end
  188.  CALL ?ne
  189.  JNZ ??5
  190.  JMP ?12
  191. ??5:
  192.  MOV AX,1
  193.  MOV DGRP:_ok2send,AX
  194.  JMP ?13
  195. ?12:
  196.  MOV AX,0
  197.  MOV DGRP:_ok2send,AX
  198. ?13:
  199.  JMP ?14
  200. ?10:
  201.  MOV AX,1
  202.  MOV DGRP:_ok2send,AX
  203. ?14:
  204.  JMP ?3
  205. ?15:
  206.  MOV AX,DGRP:_g_com
  207.  ADD AX,6
  208.  PUSH AX
  209.  CALL _in
  210.  ADD SP,2
  211.  JMP ?3
  212. ?4:
  213.  DW ?15,0,?9,1,?8,6,?6,2,?5,3,0
  214.  DW ?3
  215. ?3:
  216.  JMP ?1
  217. ?2:
  218.  MOV AX,33
  219.  PUSH AX
  220.  CALL _in
  221.  ADD SP,2
  222.  MOV -1[BP],AL
  223.  MOV AX,33
  224.  PUSH AX
  225.  MOV AL,-1[BP]
  226.  AND AL,DGRP:_pic_mask
  227.  XOR AH,AH
  228.  PUSH AX
  229.  CALL _out
  230.  ADD SP,4
  231.  add sp,1
  232.  ISR_EPILOG
  233. CSEG ENDS
  234.  EXTRN _tempflag:WORD
  235.  EXTRN _pic_mask:BYTE
  236.  EXTRN _g_out_end:WORD
  237.  EXTRN _g_in_end:WORD
  238.  EXTRN _g_out_beg:WORD
  239.  EXTRN _ok2send:WORD
  240.  EXTRN _g_com:WORD
  241.  EXTRN _g_serial_in:BYTE
  242.  EXTRN _g_serial_out:BYTE
  243.  EXTRN _in:NEAR
  244.  EXTRN _out:NEAR
  245.  END
  246. ============================ end COMISR.ASM =======================
  247. ============================ begin COMISR.C ========================
  248. #include <stdio.h>
  249. #include "com.h"
  250.  
  251. extern int tempflag;
  252.  
  253. extern unsigned char pic_mask;
  254. extern int OUTEND, INEND, OUTST, OUTEND, ok2send;
  255. extern int g_com;
  256. extern char BUFIN[BUFSIZE], BUFOUT[BUFSIZE];
  257.  
  258. unsigned int isr_off()
  259. {
  260.     asm " mov AX, OFFSET _isr";
  261. }
  262.  
  263. unsigned int isr_seg()
  264. {
  265.     asm " mov AX, SEG _isr"; 
  266. }
  267.  
  268. isr()
  269. {
  270.     unsigned char pic;
  271.  
  272.     /* PROLOG, save registers, disable interrupts */
  273.     asm " ISR_PROLOG";
  274.  
  275.     tempflag = 1;
  276.     /* tell the PIC that int is being serviced */
  277.     pic = in(0x21);
  278.     out(0x21, pic | (~pic_mask));
  279.     out(0x20, 0x20);
  280.  
  281.     /* check UART to see if it did the interrupt */
  282.     pic = in(g_com + 2);
  283.  
  284.     /* service the interupt */
  285.     while(!(pic & 1))      /* while interrupt caused by UART */
  286.     {
  287.         switch((pic & 14) >> 1)       /* isolate bits 3, 2, and 1 */
  288.         {
  289.             case 3:     /* error condition caused int */
  290.                 in(g_com + 5);  /* read LSR to reset uart */
  291.                 break;
  292.  
  293.             case 2:     /* RBF full (char ready) caused int */
  294.             {
  295.                 if(++INEND > BUFSIZE - 1)       /* wrap buffer index */
  296.                     INEND = -1;
  297.  
  298.                 INEND++;        /* "point" to THIS char */
  299.  
  300.                 BUFIN[INEND] = in(g_com);   /* add char to buffer */
  301.                 /* read RBR to reset uart */
  302.  
  303.                 break;
  304.             }
  305.  
  306.             case 6:     /* FIFO stuff...ignore */
  307.                 in(g_com);  /* read RBR, reset UART */
  308.                 break;
  309.  
  310.             case 1:     /* THR empty...ready for new char, send next one */
  311.             {
  312.                 if(OUTST != OUTEND)     /* if buffer has something in it */
  313.                 {
  314.                     if(++OUTST > BUFSIZE - 1)
  315.                         OUTST = -1;         /* wrap buffer index */
  316.  
  317.                     OUTST++;        /* point to first char */
  318.  
  319.                     out(g_com, BUFOUT[OUTST]);      /* send next char */
  320.  
  321.                     if(OUTST != OUTEND)     /* if buffer empty now */
  322.                         ok2send = 1;
  323.                     else
  324.                         ok2send = 0;
  325.                 }
  326.                 else
  327.                     ok2send = 1;
  328.  
  329.                 break;
  330.              }
  331.  
  332.             case 0:     /* modem status change */
  333.                 in(g_com + 6);
  334.         } /* end of switch */
  335.     } /* end of while */
  336.  
  337.     /* tell the PIC that int is done being serviced */
  338.     pic = in(0x21);
  339.     out(0x21, pic & pic_mask);
  340.  
  341.     asm " ISR_EPILOG";
  342. }
  343. ============================ end COMISR.C =========================
  344.  
  345. ==============================================
  346. From: Mike Dorman
  347. mikedorman@cedarnet.com
  348. 1:283/119.1012@fidonet
  349. http://www.cedarnet.com/bbs/users/mikedorman/
  350. ==============================================
  351.